package com.casic.common.utils;

import java.sql.Clob;
import java.util.HashMap;

@SuppressWarnings("serial")
public class ExpandHashMap<K, V> extends HashMap<K, V> {
	
	@Override
	public V put(K key, V value) {
		K newKey = key;
		if(null!=key&&key instanceof String){
			String keyStr = (String)newKey;
			if(!super.containsKey(keyStr)){
				newKey = (K)CommonUtil.getFieldName(keyStr, "_","");
			}
		}
		if(null!=value){
			V newValue = value; 
			if (value instanceof Clob) {
				
				String clobStr = "";
			    Clob clob = (Clob)value;
			    if (clob != null) {
			    	try{
			    		int size = (int) clob.length();
			    		clobStr = clob.getSubString(1, size);
			    	}catch(Exception e){
			    		;
			    	}
			    	newValue = (V) clobStr;
			      
			    }
				
			}
			return super.put(newKey, newValue);
		}else{
			return null;
		}
		
	}
	@Override
	public String toString() {
		String result = "";
		StringBuffer buffer = new StringBuffer();
		for (Entry<K, V> entry : super.entrySet()) {
			buffer.append("\"");
			buffer.append(entry.getKey().toString());
			buffer.append("\"");
			buffer.append(":");
			buffer.append("\"");
			buffer.append(entry.getValue().toString());
			buffer.append("\"");
			buffer.append(",");
		}  
		String data = buffer.toString();
		if("".equals(data)&&",".equals(data.substring(data.length()-1, data.length()))){
			data = data.substring(0, data.length()-1);
		}
		result = "{"+data+"}";
		return result;
		
	}
}
